package com.woniuxy.food.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.cloud.mall95.log.Log;
import com.woniuxy.food.client.foodspuclient.FoodSpuQueryClient;
import com.woniuxy.food.client.param.SearchFoodSpuByConditionParam;
import com.woniuxy.food.client.result.FoodPropClientDto;
import com.woniuxy.food.client.result.FoodPropValueClientDto;
import com.woniuxy.food.client.result.FoodSkuClientDto;
import com.woniuxy.food.client.result.FoodSpuClientDto;
import com.woniuxy.food.infra.model.FoodSpu;
import com.woniuxy.food.service.FoodSpuService;
import com.woniuxy.food.service.servicedto.FoodPropServiceDto;
import com.woniuxy.food.service.servicedto.FoodSpuServiceDto;
import com.woniuxy.food.service.serviceparam.SearchFoodSpuByConditionServiceParam;
import common.WoniuPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import result.Result;

import java.util.List;

@RestController
public class FoodSpuQueryClientApiImpl implements FoodSpuQueryClient {
    @Autowired
    private FoodSpuService foodSpuService;

    @Override
    @Log("根据条件查询菜品spu")
    public Result<WoniuPage<FoodSpuClientDto>> searchFoodSpuByCondition(SearchFoodSpuByConditionParam param) {
        SearchFoodSpuByConditionServiceParam searchFoodSpuByConditionServiceParam = BeanUtil.toBean(param, SearchFoodSpuByConditionServiceParam.class);
        WoniuPage<FoodSpuServiceDto> woniuPage = foodSpuService.searchFoodSpuByCondition(searchFoodSpuByConditionServiceParam);
        WoniuPage<FoodSpuClientDto> page = BeanUtil.toBean(woniuPage, WoniuPage.class);
        List<FoodSpuClientDto> foodSpuClientDtos = BeanUtil.copyToList(woniuPage.getList(), FoodSpuClientDto.class);
        page.setList(foodSpuClientDtos);
        for(int i=0;i<woniuPage.getList().size();i++){
            List<FoodPropClientDto> foodPropClientDtos = BeanUtil.copyToList(woniuPage.getList().get(i).getFoodPropServiceDtos(), FoodPropClientDto.class);
            foodSpuClientDtos.get(i).setFoodPropClientDtos(foodPropClientDtos);
            for(int j=0;j<woniuPage.getList().get(i).getFoodPropServiceDtos().size();j++){
                List<FoodPropValueClientDto> foodPropValueClientDtos = BeanUtil.copyToList(woniuPage.getList().get(i).getFoodPropServiceDtos().get(j).getFoodPropValueServiceDtos(), FoodPropValueClientDto.class);
                foodSpuClientDtos.get(i).getFoodPropClientDtos().get(j).setFoodPropValueClientDtos(foodPropValueClientDtos);
            }
        }
        return Result.success(page);
    }

    @Override
    @Log("根据菜品skuID查询菜品spu")
    public Result<FoodSpuClientDto> searchFoodSpuBySkuId(Integer skuId) {
        FoodSpuServiceDto foodSpuServiceDto = foodSpuService.searchFoodSpuBySkuId(skuId);
        FoodSpuClientDto foodSpuClientDto = BeanUtil.toBean(foodSpuServiceDto, FoodSpuClientDto.class);
        return Result.success(foodSpuClientDto);
    }

    @Override
    @Log("根据商家ID查询菜品spu")
    public Result<List<FoodSpuClientDto>> getSpuByStoreId(Integer storeId) {
        List<FoodSpuServiceDto> spuByStoreId = foodSpuService.getSpuByStoreId(storeId);
        List<FoodSpuClientDto> foodSpuClientDtos = BeanUtil.copyToList(spuByStoreId, FoodSpuClientDto.class);
        return Result.success(foodSpuClientDtos);
    }
}
